package com.zhiyou100.api.io_demo;

import java.io.*;

/**
 * 缓冲流：
 *  字节缓冲流：BufferedInputStream     BufferedOutputStream
 *  字符缓冲流：BufferedReader   BufferedWriter
 */
public class BufferDemo {

    public static void main(String[] args) {
        testWriter();
    }

    public static void testWriter() {
        //目标文件
        File file = new File("C:\\Users\\Administrator\\Desktop\\总部317\\api2.md");
        /*if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/
        //写入内容   将api.md文件中的内容写入到api2.md文件中
        //源文件
        File srcFile = new File("C:\\Users\\Administrator\\Desktop\\总部317\\api.md");
        BufferedReader br = null;
        //输出流
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(srcFile));
            bw = new BufferedWriter(new FileWriter(file));
            char[] chars = new char[512];
            //读取内容  一次读512个   返回值：表示读取的个数，读取完毕会返回-1
            int i = br.read(chars);
            while (i != -1) {
//                System.out.println(new String(chars));
                //写入操作
                bw.write(chars, 0, i);
                i = br.read(chars);
            }
            //刷新缓冲区
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }


}
